<?php

namespace NewMember\model;


require_once("common/Filter.php");
require_once("login/model/UserCredentials.php");


class ValidateNewUsersCredentials
{

	/**
	 * @var common\Filter
	 */
	private $filter;

	/**
	 * @var login\model\UserList
	 */
	private $userList;
	/**
	 * @var common\model\PHPFileStorage
	 */
	private $phpFileStorage;


	public function __construct()
	{
		$this->filter = new \common\Filter();
		$this->userList = new \login\model\UserList();
		$this->phpFileStorage = $this->userList->getPHPFileStorage();
	}



	// SÄTT EN SESSION I DEN HÄR KLASSEN FÖR CREATE MEMBER ERROR.
	private static $sessionLocation = "CreateErrorMessage";

	public function ValidateAndSaveCredentials($username, $password, $password2)
	{
		$this->setOrAddCreateErrorMessage("", false);

		$validUsername = $this->ValidateUsername($username);
		$validPassword = $this->ValidatePassword($password, $password2);

		$_username = $this->filter->sanitizeString($username);
		$this->setCreateUsernameSession($_username);

		if(!$this->hasTags($username, $password))
		{
			if($validUsername === true && $validPassword === true)
			{
				return true;
			}
			else
			{
				//TODO: spara username till session.
				$this->setCreateUsernameSession($username);
				return false;
			}
		}
		else
		{	
			//TODO: spara username till session men ta bort tags först.
			return false;
		}
	}


	private function setCreateUsernameSession($value)
	{
		$_SESSION['createUsernameSession'] = $value;
	}

	public function getCreateUsernameSession()
	{
		if(isset($_SESSION['createUsernameSession']))
		{
			return $_SESSION['createUsernameSession'];
		}
		else
		{
			return "";
		}
	}

	private function hasTags($username, $password)
	{
		$usernameHasTags = $this->filter->hasTags($username);
		$passwordHasTags = $this->filter->hasTags($password);
		if($usernameHasTags === true)
		{
			$this->setOrAddCreateErrorMessage("Användarnamnet innehåller ogiltiga tecken", false);
			return true;
		}
		elseif($passwordHasTags === true)
		{
			$this->setOrAddCreateErrorMessage("Lösenordet innehåller ogiltiga tecken", false);
			return true;
		}

		return false;
	}

	//
	public function createUserCredentials($username, $password)
	{
		$_username = $this->filter->sanitizeString($username);
		$_password = $this->filter->sanitizeString($password);

		$username = new \login\model\UserName($_username);
		$password = \login\model\Password::fromCleartext($_password);
	 	$newMember = \login\model\UserCredentials::create($username, $password);
		return $newMember;
	}



	public function isUsernameTaken(\login\model\UserCredentials $credentials)
	{
		if($this->userList->doesUsernameExist($credentials))
		{
			$this->setOrAddCreateErrorMessage("Användarnamnet är upptaget", false);
			return true;
		}
		return false;
	}


	public function saveNewMember(\login\model\UserCredentials $userCredentials)
	{	
		$this->phpFileStorage->writeItem($userCredentials->getUserName(),
										 $userCredentials->toString());

		//$this->phpFileStorage->readAll(); 
	}





	private function ValidateUsername($username)
	{
		$retValue = true;

		if(strlen($username) < 3)
		{
			$this->setOrAddCreateErrorMessage("Användarnamnet måste vara minst 3 tecken långt", true);
			$retValue = false;
		}

		if(strlen($username) > 9)
		{
			$this->setOrAddCreateErrorMessage("Användarnamnet får max vara 9 tecken", true);	
			$retValue = false;
		}
		return $retValue;
	}




	private function ValidatePassword($password, $password2)
	{
		$retValue = true;
		if($password != $password2)
		{	
			$this->setOrAddCreateErrorMessage("De två lösenorden du har angett stämmer ej överrens", true);
			$retValue = false;	
		}

		if(strlen($password) < 6 && strlen($password2) < 6)
		{
			$this->setOrAddCreateErrorMessage("Lösenordet måste minst vara 6 tecken", true);
			$retValue = false;
		}

		if(strlen($password) > 16 && strlen($password2) > 16)
		{
			$this->setOrAddCreateErrorMessage("Lösenordet får max vara 16 tecken", true);
			$retValue = false;
		}
			return $retValue;
	}




	private function setOrAddCreateErrorMessage($message, $add)
	{
		if($add === true)
		{
			$_SESSION[self::$sessionLocation] .= " -> " . $message;
		}
		else
		{
			$_SESSION[self::$sessionLocation] = $message;
		}
	}




	public function getCreateErrorMessage()
	{
		if(isset($_SESSION[self::$sessionLocation]) && $_SESSION[self::$sessionLocation] != "")
		{
			return htmlspecialchars($_SESSION[self::$sessionLocation]);
		}
		return "";
	}

}
